package com.tuanproject.util;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class SendMailUtil {

	private JavaMailSender mailSender;
	private VelocityEngine velocityEngine;

	public void setSimpleMailMessage(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void sendMail(String dear, final Map<String, Object> uiModel) {

		 MimeMessagePreparator preparator = new MimeMessagePreparator() {
	            public void prepare(MimeMessage mimeMessage) throws Exception {
	                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
	                message.setTo("minhtuanptithcm@gmail.com");
	                message.setFrom("tqv1111@gmail.com");
	                message.setSubject("Report");
	                String data = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "report-template/events.vm","UTF-8", uiModel);
	                message.setText(data, true);
	            }
	        };
		mailSender.send(preparator);
	}
}
